﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace timestamp
{
	class Program
	{
		[STAThread]
		static void Main(params string[] args)
		{
			if (args.Contains("-?"))
			{
				Console.WriteLine(Resources.Help);
				return;
			}

			Options options = Options.CreateOptions(args);
			Output(options);
		}

		static void Output(Options options)
		{
			string dateFormated;

			try
			{
				dateFormated = DateTime.Now.ToString(options.Format);
			}
			catch (ArgumentOutOfRangeException argumentOutOfRangeException)
			{
				HandleException(argumentOutOfRangeException);
				return;
			}

			try
			{
				Clipboard.SetText(dateFormated, TextDataFormat.Text);

				Console.WriteLine(dateFormated);

				if (!string.IsNullOrEmpty(options.Test))
				{
					Console.WriteLine(options.Test);
				}

				Thread.Sleep(3000);
			}
			catch (ExternalException externalException)
			{
				HandleException(externalException);
				return;
			}
		}

		static void HandleException(Exception exception)
		{
			Console.WriteLine("ERROR: {0} \n  {1}", exception.GetType().Name, exception.Message);
		}
	}
}
